package cn.leyaber.hrm.web.controller;

import cn.leyaber.hrm.domain.Comment;
import cn.leyaber.hrm.domain.Wholename;
import cn.leyaber.hrm.query.CommentQuery;
import cn.leyaber.hrm.service.ICommentService;
import cn.leyaber.hrm.service.IWholenameService;
import cn.leyaber.hrm.util.AjaxResult;
import cn.leyaber.hrm.util.PageList;
import cn.leyaber.hrm.vo.CommentVo;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    public ICommentService commentService;
    @Autowired
    public IWholenameService iWholenameService;
    /**
     * 批量删除
     */
    @PatchMapping("/batchDelete")
    public AjaxResult batchDelete(@RequestBody List<Long>ids){
        try {
            commentService.batchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("修改失败！"+e.getMessage());
        }
    }

    /**
     * 修改状态
     * @param comment
     * @return
     */
   @PostMapping("/Modify_status")
    public AjaxResult Modify_status(@RequestBody  Comment comment){
        try {
            commentService.Modify_status(comment);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("修改状态失败！"+e.getMessage());
        }
    }

    /**
    * 保存和修改公用的
    * @param comment  传递的实体
    * @return Ajaxresult转换结果
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public AjaxResult save(@RequestBody Comment comment){
        try {
            Comment comment1 = new Comment();
            long time = new Date().getTime();
            comment1.setTime(time);
            commentService.insert(comment1);
            if(comment.getId()!=null){
                commentService.updateById(comment);
            }else{
                commentService.insert(comment);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }

    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            commentService.deleteById(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }

    //获取用户
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public Comment get(@PathVariable("id")Long id)
    {
        return commentService.selectById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public List<Comment> list(){

        return commentService.selectList(null);
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public PageList<CommentVo> json(@RequestBody CommentQuery query)
    {
        Page<Comment> page = new Page<Comment>(query.getPage(),query.getRows());
        page = commentService.selectPage(page);
        Page<CommentVo> page1 = JSON.parseObject(JSON.toJSONString(page), Page.class);
        ArrayList<CommentVo> objects = new ArrayList<>();
        for (Comment record : page.getRecords()) {
            CommentVo commentVo = JSON.parseObject(JSON.toJSONString(record), CommentVo.class);
            if (StringUtils.isEmpty(record.getUserWholenameId())){
                objects.add(commentVo);
                continue;
            }
            Wholename wholename = iWholenameService.selectById(record.getUserWholenameId());
            commentVo.setUser(wholename.getUser());
            objects.add(commentVo);
        }
        page1.setRecords(objects);
        return new PageList<CommentVo>(page1.getTotal(),page1.getRecords());
    }
}
